import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';

import '../common/api_service.dart';
import '../common/constants.dart';
import '../model/pipe_model.dart';
import '../pages/pipe_data_list_page.dart';
import '../utils/screen_util.dart';

class CartoonInfoPipeListView extends StatefulWidget {
  final String cartoonId;

  const CartoonInfoPipeListView({super.key, required this.cartoonId});

  @override
  State<CartoonInfoPipeListView> createState() => _CartoonInfoPipeListViewState();
}

class _CartoonInfoPipeListViewState extends State<CartoonInfoPipeListView> {
  final List<PipeModel> _pipeList = [];

  @override
  void initState() {
    super.initState();

    _getUserPipeList();
  }

  void _getUserPipeList() {
    ApiService().getCartoonPipeList(widget.cartoonId, (list) {
      setState(() {
        _pipeList.clear();
        _pipeList.addAll(list);
      });
    }, () {});
  }

  @override
  Widget build(BuildContext context) {
    final screenWidth = ScreenUtil.getScreenWidth(context);
    var width = (screenWidth - 12.0) * 0.5;
    var height = width * 16 / 11;
    var h = height + 70;
    double ratio = width / h;
    return _pipeList.isNotEmpty
        ? MasonryGridView.count(
            padding: const EdgeInsets.all(4),
            crossAxisCount: 2,
            mainAxisSpacing: 8,
            crossAxisSpacing: 4,
            itemCount: _pipeList.length,
            shrinkWrap: true,
            itemBuilder: (context, index) {
              var pipe = _pipeList[index];
              return HomePipeCard(width: width, height: height, pipe: pipe, showSummary: true);
            },
          )
        : Center(
            child: LoadingAnimationWidget.progressiveDots(
              color: Constants.themeColor,
              size: 30,
            ),
          );
  }
}
